package org.smr.dev.utils;

public class NamesUtils {

    /**
     * 把表名转换成实体名
     * @param tableName
     */
    public static String tableName2EntityName(String tableName){

        String name = columnName2PropertyName(  tableName );
        String firstChart = name.substring(0,1).toUpperCase();
        String after =  name.substring(1);
        return firstChart+after;
    }


    /**
     * 实体名转换成VO名
     * @param entityName
     * @return
     */
    public static String entityName2VOName(String entityName ){
        return entityName + "VO";
    }

    /**
     * 实体名转换成QueryVO名
     * @param entityName
     * @return
     */
    public static String entityName2QueryVOName(String entityName ){
        return entityName + "QueryVO";
    }


    /**
     * 把列名转换成类属性名
     * @param columnName
     */
    public static String columnName2PropertyName( String columnName ){

        if(StringUtils.isEmpty(columnName)){
            return columnName;
        }
        //columnName = columnName.toLowerCase();
        //以"t_"开头去掉
        if(columnName.startsWith("t_")){
            columnName =  columnName.substring(2);
        }
        //以'_'结尾的去掉
        if(columnName.endsWith("_")){
            columnName = columnName.substring(0,columnName.length() - 1);
        }
        while ( columnName.contains("_") ){

            int index =  columnName.indexOf("_");
            if( index + 1 < columnName.length() ){
                String charAt = columnName.substring(index+1,index+2).toUpperCase();
                String before = columnName.substring(0,index);
                String after = columnName.substring(index+2);
                //columnName.
                columnName = before + charAt + after;
            }
            if(!columnName.contains("_")){
                break;
            }
        }
        return columnName;
    }


    /**
     * 把列名转换成类属性名
     * @param tableName
     */
    public static String tableName2UrlParts( String tableName ){

        if(StringUtils.isEmpty(tableName)){
            return tableName;
        }
        //columnName = columnName.toLowerCase();
        //以"t_"开头去掉
        if(tableName.startsWith("t_")){
            tableName =  tableName.substring(2);
        }
        //以'_'结尾的去掉
        if(tableName.endsWith("_")){
            tableName = tableName.substring(0,tableName.length() - 1);
        }

        tableName =  tableName.replace("_", "-");


        /*while ( tableName.contains("_") ){
            int index =  tableName.indexOf("_");
            if( index + 1 < tableName.length() ){
                String charAt = tableName.substring(index+1,index+2).toUpperCase();
                String before = tableName.substring(0,index);
                String after = tableName.substring(index+2);
                //columnName.
                tableName = before + charAt + after;
            }
            if(!tableName.contains("_")){
                break;
            }
        }*/
        return tableName;
    }


    /**
     * 驼峰命名
     * @param name
     * @return
     */
    public static String getCamelCaseName(String name){

        if(StringUtils.isEmpty(name)){
            return name;
        }
        String firstChar = name.substring(0,1);
        String nameTmp = name.substring(1);

        name = firstChar.toLowerCase() + nameTmp;
        return name;
    }

    /**
     * 首字母大写
     * @param name
     * @return
     */
    public static String getFirstUpperCaseName(String name){
        return tableName2EntityName(name);
    }


    public static void main(String[] args) {

        String mmm = NamesUtils.columnName2PropertyName("organ_authority_");
        System.out.println(mmm);

        String mmm1 = NamesUtils.tableName2EntityName("organ_authority_");
        System.out.println(mmm1);

        System.out.println( NamesUtils.tableName2UrlParts("t_organ_authority_wms_dd"));

    }

}
